﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class UCChuyenDen : UserControl
    {
        public int mashk;
        public UCChuyenDen()
        {
            InitializeComponent();
        }
        //private void btnTimSoChuyenKhau_Click(object sender, EventArgs e)
        //{
        //    int SoChuyenKhau = int.Parse(txtSoChuyenKhau.Text);

        //    List<ChuyenKhauPublic> list_cd = ChuyenKhauBLL.LoadChuyenKhauByMaNK(SoChuyenKhau);

        //    dgvChuyenDen.DataSource = list_cd;

        //    //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
        //    dgvChuyenDen.Columns[1].HeaderText = "Họ và tên";
        //    //dgvChuyenDen.Columns[3].HeaderText = "Tên gọi khác";
        //    dgvChuyenDen.Columns[4].HeaderText = "CMND";
        //    dgvChuyenDen.Columns[5].HeaderText = "Ngày sinh";
        //    //dgvChuyenDen.Columns[6].HeaderText = "Nơi sinh";
        //    dgvChuyenDen.Columns[7].HeaderText = "Giới tính";
        //    dgvChuyenDen.Columns[8].HeaderText = "Quê quán";
        //    dgvChuyenDen.Columns[9].HeaderText = "Dân tộc";
        //    dgvChuyenDen.Columns[10].HeaderText = "Nghề nghiệp";
        //    dgvChuyenDen.Columns[11].HeaderText = "Quốc tịch";
        //    //dgvChuyenDen.Columns[12].HeaderText = "Chứng tử";
        //    dgvChuyenDen.Columns[13].HeaderText = "Tôn giáo";
        //    dgvChuyenDen.Columns[14].HeaderText = "Trình độ";
        //    //dgvChuyenDen.Columns[15].HeaderText = "Sổ hộ khẩu";
        //    dgvChuyenDen.Columns[16].HeaderText = "Quan hệ";
        //    dgvChuyenDen.Columns[0].Visible = false;
        //    //dgvChuyenDen.Columns[1].Visible = false;
        //    dgvChuyenDen.Columns[2].Visible = false;
        //    dgvChuyenDen.Columns[3].Visible = false;
        //    //dgvChuyenDen.Columns[4].Visible = false;
        //    //dgvChuyenDen.Columns[5].Visible = false;
        //    dgvChuyenDen.Columns[6].Visible = false;
        //    //dgvChuyenDen.Columns[7].Visible = false;
        //    //dgvChuyenDen.Columns[8].Visible = false;
        //    dgvChuyenDen.Columns[9].Visible = false;
        //    //dgvChuyenDen.Columns[10].Visible = false;
        //    dgvChuyenDen.Columns[11].Visible = false;
        //    dgvChuyenDen.Columns[12].Visible = false;
        //    dgvChuyenDen.Columns[13].Visible = false;
        //    dgvChuyenDen.Columns[14].Visible = false;
        //    dgvChuyenDen.Columns[15].Visible = false;
        //    //dgvChuyenDen.Columns[16].Visible = false;
        //}

        private void cmbNguoiNhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuChuyenDen.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuChuyenDen.Enabled = false;
        }

        //private void dgvChuyenDen_SelectionChanged(object sender, EventArgs e)
        //{
        //    //NhanKhauPublic nk=new NhanKhauPublic();
        //    DataGridViewCell cell = null;

        //    foreach (DataGridViewCell select in dgvChuyenDen.SelectedCells)
        //    {
        //        cell = select;
        //    }
        //    if (cell != null)
        //    {
        //        DataGridViewRow row = cell.OwningRow;

        //        NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaNK((int)row.Cells[1].Value);
        //        //txtHoTen.Text=
        //        txtSoChuyenDi.Text = (string)row.Cells[0].Value;
        //        txtSoSHKDen.Text = (string)row.Cells[3].Value;
        //        //txtChuHoChuyenDen.Text=(string)row

        //    }

        //}

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (rdtLoai1.Checked == true)
            {
                if (txtHoTen.Text == "" || txtQueQuan.Text == "" || txtSoChuyenDi.Text == "" || txtChuHo.Text == "" || txtLyDoChuyen.Text == "" || txtNguoiCap.Text == "" ||
                    dateNgayCap.Text == "" || dateNgayCap.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ChuyenKhau();
                btnTim_Click(sender, e);
            }
            else
            {
                SoHoKhauPublic shk = new SoHoKhauPublic();
                shk.MaSHK = DungChung.LoadMaSHK();
                mashk = shk.MaSHK;
                shk.SoSoHoKhau = shk.MaSHK;
                shk.KhuPho = byte.Parse(txtAp.Text);
                shk.SoNha = txtSoNha.Text;
                shk.Duong = txtDuong.Text;
                shk.NgayCap = dateNgayNhap.Value;
                shk.NguoiCap = (int)cmbNguoiNhan.SelectedValue;
                shk.ChucVuNguoiCap = (byte)cmbChucVuNguoiCap.SelectedValue;
                shk.CanBoDangKi = frmDangNhap.user_login.MaND;
                SoHoKhauBLL.InsertSoHoKhau(shk);
                ChuyenKhau();
                txtSoSHK.Text = shk.SoSoHoKhau.ToString();
                btnTim_Click(sender, e);
                //NhanKhauBLL.UpdateMaSHK(shk.MaSHK, _MaNK);
            }

        }
        int _MaNK;
        private void ChuyenKhau()
        {
            NhanKhauPublic nk = new NhanKhauPublic();
            _MaNK = DungChung.LoadMaNK();
            nk.MaNK = DungChung.LoadMaNK();
            nk.HoTen = txtHoTen.Text;
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.TenGoiKhac = txtTenGoiKhac.Text;
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            if (txtCMND.Text != "")
            {
                nk.CMND = txtCMND.Text;
            }
            nk.TenGoiKhac = txtTenGoiKhac.Text;
            nk.MaTG = (byte)cmbTonGiao.SelectedValue;
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            nk.MaDT = (byte)cmbDanToc.SelectedValue;
            nk.NoiSinh = (int)cmbNoiSinh.SelectedValue;
            //nk.MaSHK = SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHKDen.Text)).MaSHK;
            nk.MaSHK = mashk;
            nk.MaTD = 1;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaQH = (byte)cmbQuanHe.SelectedValue;
            nk.NgayNhap = dateNgayNhap.Value;
            nk.LyDoNhap = 7;
            NhanKhauBLL.InsertNhanKhau(nk);
            //frmChuyenDen_Load(sender, e);

            //themChuyenKhau
            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            ck.SoChuyenKhau = int.Parse(txtSoChuyenDi.Text);
            ck.LyDoChuyen = txtLyDoChuyen.Text;

            ck.NgayCap = DateTime.Parse(dateNgayCap.Text);
            ck.MaNK = nk.MaNK;
            ck.ChuHo = txtChuHo.Text;

            ck.NgayNhan = dateNgayNhap.Value;
            ck.NguoiCap = txtNguoiCap.Text;
            ck.NguoiNhan = cmbNguoiNhan.Text;
            ck.NoiCap = (int)cmbPhuongXa.SelectedValue;
            ck.MaCK = DungChung.LoadMaCK();

            ck.CanBoNoiNhan = cmbCanBoDangKi.Text;
            ck.ChucVuNguoiCap = (byte)cmbChucVuNguoiCap.SelectedValue;
            ck.ChucVuNguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV;
            ck.CanBoNoiCap = "";
            ck.MaQH = (byte)cmbQuanHe.SelectedValue;
            ck.ThuongTru = txtThuongTru.Text;
            ck.NoiChuyen = ThietLapBLL.LoadMaDiaChi();
            ChuyenKhauBLL.InsertChuyenKhau(ck);
            MessageBox.Show("Thêm chuyển đến thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //UCChuyenDen_Load(sender, e);
            //btnTim_Click(sender, e);
            txtHoTen.ResetText();
            dateNgaySinh.ResetValue();
            cmbNoiSinh.Refresh();
            txtTenGoiKhac.ResetText();
            txtCMND.ResetText();

            txtQueQuan.ResetText();
            txtThuongTru.ResetText();

            cmbQuocTich.Refresh();
            cmbTonGiao.Refresh();
            cmbDanToc.Refresh();
            txtSoChuyenDi.ResetText();
            txtLyDoChuyen.ResetText();
            txtNguoiCap.ResetText();
            cmbQuanHuyen.SelectedIndex = -1;
            cmbPhuongXa.SelectedIndex = -1;
            cmbTinh.SelectedIndex = -1;
            cmbNoiSinh.Refresh();
            cmbDanToc.Refresh();
            dateNgayCap.ResetValue();

        }
        private void cmbTinh_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((int)cmbTinh.SelectedValue);
            cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyen.ValueMember = "MaQH";
            cmbQuanHuyen.SelectedIndex = -1;
        }

        private void cmbQuanHuyen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbPhuongXa.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((int)cmbQuanHuyen.SelectedValue);
            cmbPhuongXa.DisplayMember = "TenPhuongXa";
            cmbPhuongXa.ValueMember = "MaPX";
            cmbPhuongXa.SelectedIndex = -1;
        }

        private void UCChuyenDen_Load(object sender, EventArgs e)
        {   
            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen;

            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            txtChucVuChuyenDen.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;

            dateNgayNhap.Text = DateTime.Now.ToString();

            cmbTinh.DataSource = TinhBLL.LoadTinh();
            cmbTinh.DisplayMember = "TenTinh";
            cmbTinh.ValueMember = "MaT";

            cmbTinh.SelectedIndex = -1;
            cmbTinh.SelectedIndex = -1;
            //cmbTinh.Text = "   --- Chọn Tỉnh ---   ";
            //cmbQuanHuyen.Text = "  --- Chọn Huyện ---  ";
            //cmbPhuongXa.Text = "    --- Chọn Xã ---   ";

            cmbQuanHe.DataSource = QuanHeBLL.LoadToanBoQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";
            cmbQuanHe.SelectedIndex = 1;

            cmbChucVuNguoiCap.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVuNguoiCap.DisplayMember = "TenChucVu";
            cmbChucVuNguoiCap.ValueMember = "MaCV";

            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";         

            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";

            cmbTonGiao.DataSource = TonGiaoBLL.LoadTonGiao();
            cmbTonGiao.DisplayMember = "TenTonGiao";
            cmbTonGiao.ValueMember = "MaTG";

            txtNoiChuyen.Text = ThietLapBLL.LoadDiaChi();

            cmbNoiSinh.DataSource = TinhBLL.LoadTinh();
            cmbNoiSinh.DisplayMember = "TenTinh";
            cmbNoiSinh.ValueMember = "MaT";

        }

        //private void cmbTinhDen_SelectionChangeCommitted(object sender, EventArgs e)
        //{
        //    cmbQuanHuyenDen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((byte)cmbTinh.SelectedValue);
        //    cmbQuanHuyenDen.DisplayMember = "TenQuanHuyen";
        //    cmbQuanHuyenDen.ValueMember = "MaQH";
        //}

        //private void cmbQuanHuyenDen_SelectionChangeCommitted(object sender, EventArgs e)
        //{
        //    cmbPhuongXaDen.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((byte)cmbQuanHuyen.SelectedValue);
        //    cmbPhuongXaDen.DisplayMember = "TenPhuongXa";
        //    cmbPhuongXaDen.ValueMember = "MaPX";
        //}

       

        //private void btnTim_Click(object sender, EventArgs e)
        //{
        //    if (txtSoSHKDen.Text == "")
        //    {
        //        MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return;
        //    }
        //    int SoSHK = int.Parse(txtSoSHKDen.Text);
        //    SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
        //    if (shk == null)
        //    {
        //        txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
        //        dgvNhanKhau.DataSource = null;
        //        groupBox1.Enabled = false;
        //        groupBox2.Enabled = false;
        //        MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //    else
        //    {
        //        //SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
        //        List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);

        //        groupBox1.Enabled = true;
        //        groupBox2.Enabled = true;
        //        txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
        //        dgvNhanKhau.DataSource = list_nk;
        //        dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
        //        dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
        //        //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
        //        dgvNhanKhau.Columns[4].HeaderText = "CMND";
        //        dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
        //        //dgvNhanKhau
        //        dgvNhanKhau.Columns[6].HeaderText = "Giới tính";
        //        dgvNhanKhau.Columns[7].HeaderText = "Quê quán";
        //        dgvNhanKhau.Columns[8].HeaderText = "Dân tộc";
        //        dgvNhanKhau.Columns[9].HeaderText = "Nghề nghiệp";
        //        dgvNhanKhau.Columns[10].HeaderText = "Quốc tịch";
        //        //dgvNhanKhau.Columns[11].HeaderText = "Chứng tử";
        //        dgvNhanKhau.Columns[12].HeaderText = "Tôn giáo";
        //        dgvNhanKhau.Columns[13].HeaderText = "Trình độ";
        //        //dgvNhanKhau.Columns[14].HeaderText = "Sổ hộ khẩu";
        //        dgvNhanKhau.Columns[15].HeaderText = "Quan hệ";
        //        //dgvNhanKhau.Columns[0].Visible = false;
        //        //dgvNhanKhau.Columns[1].Visible = false;
        //        dgvNhanKhau.Columns[2].Visible = false;
        //        dgvNhanKhau.Columns[3].Visible = false;
        //        //dgvNhanKhau.Columns[4].Visible = false;
        //        //dgvNhanKhau.Columns[5].Visible = false;
        //        //dgvNhanKhau.Columns[6].Visible = false;
        //        //dgvNhanKhau.Columns[7].Visible = false;
        //        dgvNhanKhau.Columns[8].Visible = false;
        //        //dgvNhanKhau.Columns[9].Visible = false;
        //        dgvNhanKhau.Columns[10].Visible = false;
        //        dgvNhanKhau.Columns[11].Visible = false;
        //        dgvNhanKhau.Columns[12].Visible = false;
        //        dgvNhanKhau.Columns[13].Visible = false;
        //        dgvNhanKhau.Columns[14].Visible = false;
        //        //dgvNhanKhau.Columns[15].Visible = false;

        //        //load phần sổ hộ khẩu
        //        txtDuong.Text = shk.Duong;
        //        txtAp.Text = shk.KhuPho.ToString();
        //        txtSoNha.Text = shk.SoNha;
        //    }
        //    DataGridViewCellStyle style = new DataGridViewCellStyle();
        //    style.ForeColor = Color.AliceBlue;
        //    style.BackColor = Color.Red;
        //    for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
        //    {
        //        if ((int)dgvNhanKhau.Rows[i].Cells[11].Value == 0)
        //        {
        //            //MessageBox.Show("geg");
        //            dgvNhanKhau.Rows[i].DefaultCellStyle = style;
        //        }
        //        //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
        //        //{
        //        //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
        //        //}

        //    }
        //}

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        //private void txtSoChuyenKhau_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoChuyenKhau.Text.Length > 7 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtHoTen.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoChuyenDi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoChuyenDi.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {

            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                //groupBox1.Enabled = false;
                //groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                mashk=shk.MaSHK;
                //SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                groupBox4.Enabled = false;
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
                dgvNhanKhau.DataSource = list_nk;
                dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                dgvNhanKhau.Columns[2].HeaderText = "Mã KS";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                //dgvNhanKhau
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[11].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[14].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                //dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                dgvNhanKhau.Columns[8].Visible = false;
                //dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;
                dgvNhanKhau.Columns[17].Visible = false;
                dgvNhanKhau.Columns[18].Visible = false;
                dgvNhanKhau.Columns[19].Visible = false;
                dgvNhanKhau.Columns[20].Visible = false;
                dgvNhanKhau.Columns[21].Visible = false;
                dgvNhanKhau.Columns[22].Visible = false;
                dgvNhanKhau.Columns[23].Visible = false;

                //load phần sổ hộ khẩu
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;
                if (shk.NgayXoa != null)
                {
                    groupBox1.Enabled = false;
                    groupBox2.Enabled = false;
                    btnThem.Enabled = false; 
                }
                else
                    btnThem.Enabled = true; 
            }
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;
            for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
            {
                if ((byte)dgvNhanKhau.Rows[i].Cells[21].Value != 0)
                {
                    //MessageBox.Show("geg");
                    dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                }
                //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                //{
                //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                //}

            }
        }

        //private void txtNoiSinh_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtNoiSinh.Text.Length > 49 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtTenGoiKhac_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtTenGoiKhac.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtCMND.Text.Length > 8 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

       
        private void txtQueQuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtQueQuan.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtChuHoChuyenDen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtChuHo.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtLyDoChuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtLyDoChuyen.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNguoiCap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNguoiCap.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }


        private void txtCMND_Leave(object sender, EventArgs e)
        {
            if (txtCMND.Text.Length < 9)
            {
                balloonTip2.ShowBalloon(txtCMND);
                balloonTip2.SetBalloonCaption(txtCMND, "Thông báo");
                balloonTip2.SetBalloonText(txtCMND, "CMND không hợp lệ");
                txtCMND.Focus();
            }
            NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByCMND(txtCMND.Text);
            if (nk != null)
            {
                this.balloonTip2.ShowBalloon(txtCMND);
                this.balloonTip2.SetBalloonCaption(txtCMND, "Thông báo");
                this.balloonTip2.SetBalloonText(txtCMND, "Đã có CMND này trong hệ thống");
                txtCMND.Focus();
            }
        }

        private void rdtLoai1_CheckedChanged(object sender, EventArgs e)
        {   
                

                if (rdtLoai1.Checked == true)
                {
                    txtSoSHK.Enabled = true;
                    txtSoSHK.Focus();
                    cmbQuanHe.SelectedIndex = 1;
                    cmbQuanHe.Enabled = true;

                }
                else
                {
                    groupBox4.Enabled = true;
                    txtSoSHK.Enabled = false;
                    txtChuHo.Enabled = false;
                    btnTim.Enabled = true;
                    txtSoNha.Enabled = true;
                    txtSoNha.Focus();
                    txtDuong.Enabled = true;
                    txtAp.Enabled = true;
                    cmbQuanHe.Enabled = false;
                    cmbQuanHe.SelectedIndex = 0;
                    txtSoSHK.ResetText();
                    txtSoNha.ResetText();
                    txtDuong.ResetText();
                    txtChuHo.ResetText();
                    dgvNhanKhau.DataSource = null;
                    txtAp.ResetText();
                } 
        }

        private void txtSoNha_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtSoNha.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtDuong.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtAp_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtAp.Text.Length > 1 && !Char.IsControl(e.KeyChar) || !Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbQuanHe_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if ((byte)cmbQuanHe.SelectedValue == 1)
            {
                MessageBox.Show("Không thể chọn chủ hộ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbQuanHe.Focus();
            }
        }

        private void cmbPhuongXa_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtSoChuyenDi.Enabled = true;
        }

        private void txtSoChuyenDi_Leave(object sender, EventArgs e)
        {
            if (cmbPhuongXa.SelectedValue != null)
            {
                ThietLapPublic tl = ThietLapBLL.LoadThietLapByMaTL((int)cmbPhuongXa.SelectedValue);
                if (tl != null&& txtSoChuyenDi.Text!="")
                {
                    ChuyenKhauPublic ck = ChuyenKhauBLL.TimChuyenKhau(int.Parse(txtSoChuyenDi.Text),tl.TenServer, tl.TenData );
                    if (ck != null)
                    {
                        MessageBox.Show("Không tìm thấy số chuyển khẩu này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtSoChuyenDi.ResetText();
                        txtSoSHK.Enabled = false;
                        txtChuHo.Enabled = false;
                        btnTim.Enabled = true;
                        txtSoNha.Enabled = true;
                        txtSoNha.Focus();
                        txtDuong.Enabled = true;
                        txtAp.Enabled = true;
                        cmbQuanHe.Enabled = false;
                        cmbQuanHe.SelectedIndex = 0;
                        txtSoSHK.ResetText();
                        txtSoNha.ResetText();
                        txtDuong.ResetText();
                        txtChuHo.ResetText();
                        dgvNhanKhau.DataSource = null;
                        txtAp.ResetText();
                    }
                    else
                    {
                        groupBox1.Enabled = false;
                        NhanKhauPublic nk= NhanKhauBLL.TimNhanKhau(ck.MaNK,tl.TenServer,tl.TenData);
                        if (rdtLoai1.Checked == true)
                        {

                            SoHoKhauPublic shk = SoHoKhauBLL.LoadSHKByChuHo(ck.ChuHo);
                            txtSoNha.Text = shk.SoNha;
                            txtDuong.Text = shk.Duong;
                            txtAp.Text = shk.KhuPho.ToString();
                            txtChuHo.Text = ck.ChuHo;
                            txtSoSHK.Text = nk.MaSHK.ToString();
                        }
                        cmbQuanHe.SelectedValue = ck.MaQH;
                        txtLyDoChuyen.Text = ck.LyDoChuyen;
                        dateNgayCap.Value = (DateTime)ck.NgayCap;
                        txtNoiChuyen.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(ck.NoiChuyen);
                        txtNguoiCap.Text = ck.NguoiCap;
                        cmbChucVuNguoiCap.SelectedValue = ck.ChucVuNguoiCap;
                        txtHoTen.Text = nk.HoTen;
                        if (nk.GioiTinh == true)
                            rdtGioiTinhNam.Checked = true;
                        else
                            rdtGioiTinhNu.Checked = false;
                        dateNgaySinh.Value = (DateTime)nk.NgaySinh;
                        cmbNoiSinh.SelectedValue = nk.NoiSinh ;
                        txtTenGoiKhac.Text = nk.TenGoiKhac;
                        cmbTonGiao.SelectedValue = nk.MaTG;
                        cmbQuocTich.SelectedValue = nk.MaQT ;
                        cmbDanToc.SelectedValue = nk.MaDT ;
                        txtCMND.Text = nk.CMND;
                        txtQueQuan.Text = nk.QueQuan;
                        txtThuongTru.Text = ck.ThuongTru;
                        
                    }
                }
            }
        }
    }
}
